﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace hmi
{
    public delegate void CMDebugHandler(string data);
    public delegate void WrongHandler();
    class CommManage
    {        
        private bool _isUdp;

        public event CMDebugHandler SendDebugEvent;
        public event CMDebugHandler ReceiveDebugEvent;

        public static event WrongHandler WrongEvent;

        public ICommunication CommTool { get; set; }

        private static  CommManage _instance = null;

        //static CommManage()
        //{
        //    _instance = new CommManage();  
        //}

        private CommManage()
        {
            InitCMMode();
        }

        public static CommManage Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new CommManage();
                }
                return _instance;
            }
        }

        public bool IsUdp
        {
            get
            {
                return _isUdp;
            }
            set
            {
                if (_isUdp == value)
                {
                    return;//若_isUdp的值不改变，则依旧是之前的通信方式
                }
                _isUdp = value;
                if (CommTool != null)
                {
                    CommTool.Close();
                }
                CreateCommTool();
                SaveConfigure();
            }
        }

        private void SaveConfigure()
        {
            Configuration configure = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            configure.AppSettings.Settings["IsUdp"].Value = _isUdp.ToString();
            configure.Save();
            ConfigurationManager.RefreshSection("appSettings");
        }

        private void InitCMMode()
        {
            _isUdp = Convert.ToBoolean(ConfigurationManager.AppSettings["IsUdp"]);
            CreateCommTool();
        }

        private void CreateCommTool()
        {           
            if (_isUdp)
            {
                CommTool = new UdpComm();//CommTool = new UdpComm()的过程中释放了原先的CommTool                 
            }
            else
            {
                CommTool = new SerialComm();                
            }
            CommTool.Open();
            if (!CommTool.IsCommRight)
            {
                WrongEvent?.Invoke();
            }
        }

        public void SendByteData(byte[] byteData)
        {
            if (!CommTool.IsOpen)
            {
                return;
            }
            CommTool.SendByteData(byteData);
            string sendData = Utility.BytesToHexString(byteData);
            SendDebugEvent?.Invoke(sendData);//发送的原始数据交由调试界面显示
        }

        public void ReceiveBytesDebug(byte[] byteData)
        {
            string receiveData = Utility.BytesToHexString(byteData);
            ReceiveDebugEvent?.Invoke(receiveData);//收到的原始数据交由调试界面显示
        }
    }
}
